<?php

namespace app\api\controller;

use app\common\lib\ChatGpt;
use app\common\lib\Sensitive;
use app\common\model\MemberPointsLog;
use hg\apidoc\annotation as Apidoc;
use hg\apidoc\annotation\Param;
use hg\apidoc\annotation\Query;
use hg\apidoc\annotation\Returned;
use hg\apidoc\annotation\Header;
use app\BaseController;
use think\facade\View;
use think\facade\Db;
use think\response\Json;

/**
 * 文案创作
 *
 * api-Copywriting
 *
 * @Apidoc\Title("文案创作")
 * @Apidoc\Group("api")
 */
class Copywriting2 extends BaseController
{
    /**
     * getList
     * @Apidoc\Title("获取产品列表")
     * @return Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getList()
    {
        $classes = \app\common\model\CopywritingClassification::order('sort', 'asc')->select();
        $classes = $classes->toArray();
        foreach ($classes as $index => $class) {
            $classes[$index]['active'] = false;
            $classes[$index]['lists'] = \app\common\model\Copywriting::order('sort', 'asc')
                ->where('copywriting_classification_id', $class['id'])
                ->append(['active'])
                ->withAttr('active', function ($value, $data) {
                    return false;
                })
                ->select();
        }
        return success('', $classes);

    }
    /**
     * getALlList
     * @Apidoc\Title("获取所有产品列表")
     * @return Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getALlList()
    {
        $where=[];
        $keywords=input("keywords");
        if($keywords){
            $where[]=["name","like","%".$keywords."%"];
        }
        $data= \app\common\model\Copywriting::order('sort', 'asc')->where($where)->select();
        if($keywords){
            foreach($data as $key=>$v){
                $data[$key]['name']=str_replace($keywords,"<b>".$keywords."</b>",$data[$key]['name']);
                $data[$key]['describe']=str_replace($keywords,"<b>".$keywords."</b>",$data[$key]['describe']);
            }
        }
        return success('', $data);
    }


    /**
     * @Apidoc\Title("获取产品相关信息")
     * getDat
     */
    public function getData()
    {
        $id = input('id');
        $copywriting = \app\common\model\Copywriting::field(['custom_fields', 'name', 'picture', 'is_language', 'is_tone','describe'])->find($id);
        $custom_fields = $copywriting['custom_fields'];
        $fields = explode("\r\n", $custom_fields);
        $user_fields = [];
        foreach ($fields as $field) {
            if ($field) {
                $user_fields[] = explode("__", $field);
            }
        }
//        $user_data = \app\common\model\CopywritingMember::order('id', 'desc')
//            ->where('copywriting_id', $id)
//            ->where('member_id', $this->request->uid)->select();
        $tones = explode("\r\n", trim(config('article_creation.creative_tone')));
        return success('', [
            'data' => $copywriting,
            'fields' => $user_fields,
            'user_data' => [],
            'tones' => $tones
        ]);

    }

    /**
     * 获取生成语言
     * @Apidoc\Title("获取生成语言")
     * getLanguage
     */
    public function getLanguage()
    {
        $language = config('article_creation.language');
        $arr = explode("\r\n", trim($language));
        return success('', $arr);
    }
}
